Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 540)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.37566 11.43166 11.48669 11.54075 11.59386 11.64600 11.69719 11.74742
##   [9] 11.79669 11.84501 11.89239 11.93882 11.98430 12.02883 12.07243 12.11509
##  [17] 12.15681 12.19759 12.23745 12.27637 12.31434 12.35134 12.38736 12.42242
##  [25] 12.45650 12.48961 12.52175 12.55293 12.58313 12.61237 12.64064 12.66794
##  [33] 12.69428 12.71966 12.74407 12.76752 12.79000 12.81153 12.83210 12.85170
##  [41] 12.87034 12.88802 12.90473 12.92048 12.93526 12.94908 12.96193 12.97381
##  [49] 12.98473 12.99467 13.00365 13.01165 13.01869 13.02475 13.02984 13.03333
##  [57] 13.03470 13.03416 13.03189 13.02806 13.02288 13.01653 13.00920 13.00107
##  [65] 12.99233 12.98317 12.97377 12.96433 12.95503 12.94606 12.93510 12.92009
##  [73] 12.90159 12.88016 12.85636 12.83075 12.80391 12.77638 12.74873 12.72153
##  [81] 12.69534 12.67071 12.64821 12.62840 12.61185 12.59538 12.57582 12.55370
##  [89] 12.52958 12.50401 12.47752 12.45068 12.42402 12.39809 12.37344 12.35062
##  [97] 12.33018 12.31265 12.29860 12.28682 12.27573 12.26530 12.25550 12.24628
## [105] 12.23762 12.22948 12.22181 12.21460 12.20779 12.20136 12.19528 12.18950
## [113] 12.18398 12.17871 12.17363 12.16871 12.16393 12.15923 12.15494 12.15133
## [121] 12.14838 12.14605 12.14429 12.14308 12.14236 12.14210 12.14226 12.14281
## [129] 12.14370 12.14489 12.14635 12.14803 12.15019 12.15307 12.15664 12.16086
## [137] 12.16571 12.17115 12.17714 12.18366 12.19067 12.19814 12.20603 12.21431
## [145] 12.22296 12.23192 12.24118 12.25070 12.26045 12.27039 12.28049 12.29071
## [153] 12.30103 12.31306 12.32820 12.34610 12.36642 12.38882 12.41295 12.43848
## [161] 12.46505 12.49232 12.51995 12.54760 12.57491 12.60156 12.62718 12.65145
## [169] 12.67401 12.69452 12.71265 12.72803 12.74034 12.74922 12.75689 12.76568
## [177] 12.77541 12.78592 12.79703 12.80856 12.82036 12.83224 12.84404 12.85557
## [185] 12.86668 12.87718 12.88691 12.89569 12.90335 12.90973 12.91464 12.91792
## [193] 12.91939 12.91888 12.91623 12.91125 12.90378 12.89300 12.87854 12.86088
## [201] 12.84051 12.81791 12.79356 12.76795 12.74155 12.71486 12.68835 12.66252
## [209] 12.63783 12.61478 12.59385 12.57092 12.54199 12.50773 12.46882 12.42592
## [217] 12.37972 12.33088 12.28007 12.22798 12.17527 12.12261 12.07068 12.02016
## [225] 11.97170 11.92599 11.88370 11.84550 11.81207 11.78407 11.75810 11.73059
## [233] 11.70197 11.67264 11.64305 11.61360 11.58474 11.55687 11.53043 11.50583
## [241] 11.48350 11.46387 11.44735 11.43438 11.42486 11.41825 11.41427 11.41265
## [249] 11.41310 11.41536 11.41913 11.42416 11.43016 11.43685 11.44395 11.45120
## [257] 11.45831 11.46501 11.47102 11.47607 11.48255 11.49269 11.50599 11.52193
## [265] 11.53998 11.55962 11.58034 11.60162 11.62294 11.64379 11.66363 11.68196
## [273] 11.69826 11.71200 11.72518 11.74002 11.75635 11.77399 11.79277 11.81251
## [281] 11.83306 11.85422 11.87583 11.89771 11.91969 11.94160 11.96326 11.98451
## [289] 12.00516 12.02504 12.04398 12.06182 12.07836 12.09415 12.10981 12.12534
## [297] 12.14074 12.15601 12.17114 12.18612 12.20096 12.21565 12.23019 12.24457
## [305] 12.25879 12.27284 12.28672 12.30043 12.31397 12.32733 12.34050 12.35348
## [313] 12.36628 12.37887 12.39149 12.40431 12.41728 12.43036 12.44349 12.45664
## [321] 12.46974 12.48277 12.49566 12.50837 12.52085 12.53305 12.54493 12.55643
## [329] 12.56752 12.57814 12.58896 12.60056 12.61279 12.62548 12.63846 12.65158
## [337] 12.66466 12.67755 12.69008 12.70209 12.71341 12.72388 12.73333 12.74160
## [345] 12.74887 12.75545 12.76138 12.76669 12.77143 12.77564 12.77936 12.78263
## [353] 12.78550 12.78799 12.79016 12.79205 12.79368 12.79511 12.79638 12.79752
## [361] 12.79858 12.79960 12.80062 12.80068 12.79899 12.79584 12.79151 12.78628
## [369] 12.78043 12.77426 12.76804 12.76205 12.75659 12.75192 12.74835 12.74614
## [377] 12.74558 12.74561 12.74502 12.74392 12.74241 12.74060 12.73859 12.73649
## [385] 12.73439 12.73240 12.73063 12.72918 12.72815 12.72764 12.72777 12.72863
## [393] 12.73033 12.73340 12.73811 12.74417 12.75129 12.75916 12.76752 12.77605
## [401] 12.78447 12.79248 12.79981 12.80614 12.81120 12.81469 12.81631 12.81703
## [409] 12.81797 12.81907 12.82031 12.82163 12.82300 12.82438 12.82571 12.82696
## [417] 12.82809 12.82905 12.82980 12.83030 12.83051 12.83038 12.82987 12.82894
## [425] 12.82755 12.82565 12.82311 12.81989 12.81608 12.81174 12.80697 12.80185
## [433] 12.79646 12.79089 12.78521 12.77951 12.77387 12.76838 12.76311 12.75815
## [441] 12.75358 12.74949 12.74484 12.73867 12.73116 12.72249 12.71284 12.70239
## [449] 12.69133 12.67983 12.66808 12.65625 12.64454 12.63311 12.62215 12.61184
## [457] 12.60237 12.59391 12.58664 12.58075 12.57641 12.57286 12.56923 12.56560
## [465] 12.56205 12.55862 12.55541 12.55247 12.54989 12.54771 12.54603 12.54490
## [473] 12.54440 12.54459 12.54555 12.54760 12.55092 12.55536 12.56076 12.56696
## [481] 12.57382 12.58117 12.58886 12.59674 12.60465 12.61243 12.61994 12.62701
## [489] 12.63350 12.63924 12.64408 12.64864 12.65361 12.65892 12.66454 12.67042
## [497] 12.67650 12.68274 12.68908 12.69549 12.70190 12.70828 12.71457 12.72072
## [505] 12.72668 12.73261 12.73867 12.74486 12.75118 12.75762 12.76417 12.77084
## [513] 12.77761 12.78448 12.79145 12.79850 12.80564 12.81286 12.82016 12.82752
## [521] 12.83495 12.84244 12.84998 12.85756 12.86519 12.87286 12.88055 12.88824
## [529] 12.89595 12.90367 12.91143 12.91922 12.92706 12.93495 12.94290 12.95093
## [537] 12.95903 12.96722 12.97551 12.98389
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 540)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.92974 11.00064 11.07041 11.13904 11.20651 11.27282 11.33795 11.40189
##   [9] 11.46462 11.52614 11.58643 11.64548 11.70328 11.75982 11.81507 11.86904
##  [17] 11.92171 11.97306 12.02308 12.07177 12.11914 12.16521 12.21002 12.25357
##  [25] 12.29589 12.33700 12.37690 12.41563 12.45319 12.48961 12.52491 12.55909
##  [33] 12.59219 12.62421 12.65519 12.68512 12.71373 12.74075 12.76623 12.79022
##  [41] 12.81280 12.83401 12.85391 12.87255 12.89000 12.90631 12.92154 12.93573
##  [49] 12.94896 12.96128 12.97274 12.98340 12.99332 13.00255 13.01115 13.01806
##  [57] 13.02237 13.02434 13.02422 13.02229 13.01879 13.01399 13.00815 13.00154
##  [65] 12.99440 12.98702 12.97964 12.97252 12.96593 12.96013 12.95418 12.94702
##  [73] 12.93874 12.92944 12.91920 12.90811 12.89627 12.88376 12.87068 12.85712
##  [81] 12.84316 12.82891 12.81444 12.79985 12.78523 12.76865 12.74851 12.72538
##  [89] 12.69982 12.67240 12.64368 12.61424 12.58465 12.55546 12.52725 12.50058
##  [97] 12.47603 12.45415 12.43553 12.41757 12.39752 12.37571 12.35242 12.32798
## [105] 12.30268 12.27684 12.25077 12.22477 12.19915 12.17422 12.15028 12.12765
## [113] 12.10663 12.08754 12.07067 12.05634 12.04485 12.03651 12.02941 12.02157
## [121] 12.01325 12.00469 11.99612 11.98781 11.97998 11.97289 11.96678 11.96190
## [129] 11.95848 11.95678 11.95704 11.95950 11.96453 11.97221 11.98231 11.99462
## [137] 12.00894 12.02504 12.04271 12.06174 12.08193 12.10304 12.12487 12.14721
## [145] 12.16985 12.19256 12.21514 12.23738 12.25905 12.27996 12.29987 12.31859
## [153] 12.33589 12.35463 12.37750 12.40405 12.43383 12.46638 12.50126 12.53802
## [161] 12.57620 12.61535 12.65502 12.69476 12.73412 12.77265 12.80989 12.84540
## [169] 12.87872 12.90940 12.93700 12.96105 12.98112 12.99674 13.01019 13.02398
## [177] 13.03799 13.05210 13.06617 13.08011 13.09378 13.10707 13.11985 13.13202
## [185] 13.14344 13.15399 13.16357 13.17204 13.17929 13.18520 13.18965 13.19251
## [193] 13.19368 13.19302 13.19043 13.18577 13.17894 13.16941 13.15699 13.14200
## [201] 13.12480 13.10571 13.08509 13.06326 13.04057 13.01736 12.99397 12.97073
## [209] 12.94799 12.92608 12.90535 12.88271 12.85520 12.82334 12.78764 12.74861
## [217] 12.70678 12.66266 12.61676 12.56961 12.52171 12.47358 12.42575 12.37872
## [225] 12.33302 12.28915 12.24764 12.20900 12.17374 12.14238 12.11105 12.07601
## [233] 12.03797 11.99767 11.95582 11.91316 11.87039 11.82826 11.78747 11.74877
## [241] 11.71285 11.68047 11.65232 11.62915 11.60881 11.58874 11.56908 11.54993
## [249] 11.53141 11.51364 11.49672 11.48078 11.46592 11.45227 11.43994 11.42905
## [257] 11.41970 11.41202 11.40612 11.40212 11.40098 11.40331 11.40870 11.41673
## [265] 11.42698 11.43904 11.45247 11.46687 11.48182 11.49689 11.51168 11.52575
## [273] 11.53870 11.55010 11.56205 11.57677 11.59397 11.61338 11.63474 11.65778
## [281] 11.68223 11.70782 11.73427 11.76132 11.78870 11.81614 11.84336 11.87011
## [289] 11.89610 11.92107 11.94475 11.96687 11.98715 12.00717 12.02857 12.05119
## [297] 12.07486 12.09945 12.12479 12.15072 12.17708 12.20374 12.23051 12.25726
## [305] 12.28382 12.31004 12.33576 12.36083 12.38509 12.40838 12.43054 12.45143
## [313] 12.47088 12.48875 12.50545 12.52154 12.53709 12.55212 12.56669 12.58085
## [321] 12.59463 12.60809 12.62127 12.63422 12.64698 12.65960 12.67212 12.68459
## [329] 12.69706 12.70958 12.72190 12.73378 12.74526 12.75637 12.76716 12.77765
## [337] 12.78788 12.79789 12.80771 12.81739 12.82695 12.83643 12.84587 12.85530
## [345] 12.86441 12.87288 12.88075 12.88810 12.89496 12.90138 12.90742 12.91313
## [353] 12.91856 12.92376 12.92879 12.93369 12.93851 12.94331 12.94814 12.95305
## [361] 12.95808 12.96330 12.96875 12.97421 12.97941 12.98439 12.98919 12.99383
## [369] 12.99834 13.00277 13.00713 13.01146 13.01580 13.02016 13.02459 13.02912
## [377] 13.03377 13.03866 13.04386 13.04929 13.05490 13.06062 13.06639 13.07216
## [385] 13.07786 13.08343 13.08881 13.09394 13.09875 13.10318 13.10719 13.11069
## [393] 13.11363 13.11681 13.12090 13.12571 13.13102 13.13662 13.14230 13.14786
## [401] 13.15309 13.15777 13.16170 13.16467 13.16647 13.16689 13.16573 13.16324
## [409] 13.15987 13.15571 13.15081 13.14526 13.13911 13.13244 13.12533 13.11783
## [417] 13.11003 13.10199 13.09379 13.08549 13.07717 13.06890 13.06074 13.05277
## [425] 13.04506 13.03768 13.02940 13.01915 13.00720 12.99383 12.97934 12.96400
## [433] 12.94809 12.93190 12.91572 12.89981 12.88448 12.86999 12.85664 12.84470
## [441] 12.83446 12.82621 12.81870 12.81059 12.80197 12.79295 12.78362 12.77409
## [449] 12.76445 12.75481 12.74525 12.73589 12.72682 12.71814 12.70994 12.70234
## [457] 12.69543 12.68930 12.68406 12.67981 12.67664 12.67463 12.67370 12.67372
## [465] 12.67458 12.67616 12.67834 12.68099 12.68400 12.68724 12.69061 12.69397
## [473] 12.69720 12.70020 12.70283 12.70566 12.70927 12.71358 12.71851 12.72396
## [481] 12.72987 12.73613 12.74266 12.74939 12.75623 12.76308 12.76988 12.77653
## [489] 12.78295 12.78905 12.79475 12.80042 12.80644 12.81279 12.81942 12.82630
## [497] 12.83339 12.84066 12.84806 12.85556 12.86312 12.87071 12.87829 12.88582
## [505] 12.89327 12.90073 12.90835 12.91612 12.92403 12.93208 12.94027 12.94859
## [513] 12.95705 12.96563 12.97434 12.98317 12.99211 13.00117 13.01034 13.01962
## [521] 13.02900 13.03848 13.04806 13.05773 13.06750 13.07735 13.08728 13.09728
## [529] 13.10738 13.11756 13.12783 13.13820 13.14867 13.15925 13.16993 13.18073
## [537] 13.19165 13.20270 13.21387 13.22517
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 540)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.88992 10.94700 11.00316 11.05839 11.11269 11.16605 11.21848 11.26996
##   [9] 11.32050 11.37009 11.41872 11.46639 11.51310 11.55884 11.60361 11.64741
##  [17] 11.69022 11.73205 11.77290 11.81275 11.85163 11.88953 11.92647 11.96245
##  [25] 11.99748 12.03155 12.06467 12.09684 12.12807 12.15836 12.18771 12.21613
##  [33] 12.24362 12.27018 12.29582 12.32055 12.34419 12.36664 12.38792 12.40806
##  [41] 12.42710 12.44507 12.46201 12.47795 12.49292 12.50696 12.52011 12.53239
##  [49] 12.54384 12.55450 12.56439 12.57356 12.58203 12.58984 12.59703 12.60300
##  [57] 12.60725 12.60991 12.61116 12.61112 12.60996 12.60782 12.60486 12.60122
##  [65] 12.59706 12.59253 12.58777 12.58293 12.57818 12.57365 12.56769 12.55883
##  [73] 12.54744 12.53393 12.51866 12.50205 12.48447 12.46631 12.44796 12.42982
##  [81] 12.41227 12.39570 12.38050 12.36705 12.35576 12.34471 12.33193 12.31769
##  [89] 12.30228 12.28597 12.26905 12.25179 12.23447 12.21737 12.20078 12.18496
##  [97] 12.17021 12.15679 12.14500 12.13398 12.12275 12.11135 12.09979 12.08813
## [105] 12.07640 12.06463 12.05285 12.04111 12.02943 12.01786 12.00642 11.99516
## [113] 11.98411 11.97330 11.96276 11.95255 11.94268 11.93320 11.92316 11.91181
## [121] 11.89943 11.88631 11.87274 11.85901 11.84541 11.83223 11.81975 11.80827
## [129] 11.79808 11.78946 11.78271 11.77810 11.77452 11.77069 11.76670 11.76265
## [137] 11.75862 11.75471 11.75101 11.74761 11.74460 11.74207 11.74011 11.73882
## [145] 11.73829 11.73861 11.73986 11.74215 11.74556 11.75018 11.75611 11.76343
## [153] 11.77224 11.78361 11.79832 11.81604 11.83646 11.85923 11.88403 11.91054
## [161] 11.93841 11.96733 11.99697 12.02699 12.05708 12.08689 12.11610 12.14439
## [169] 12.17141 12.19686 12.22039 12.24168 12.26040 12.27622 12.29208 12.31094
## [177] 12.33246 12.35628 12.38209 12.40954 12.43828 12.46800 12.49833 12.52896
## [185] 12.55953 12.58972 12.61919 12.64758 12.67458 12.69984 12.72302 12.74379
## [193] 12.76180 12.77672 12.78822 12.79594 12.79957 12.79887 12.79423 12.78613
## [201] 12.77511 12.76167 12.74633 12.72960 12.71199 12.69401 12.67618 12.65902
## [209] 12.64303 12.62873 12.61663 12.60299 12.58410 12.56052 12.53280 12.50151
## [217] 12.46721 12.43046 12.39181 12.35183 12.31108 12.27012 12.22950 12.18980
## [225] 12.15156 12.11534 12.08172 12.05124 12.02448 12.00197 11.98101 11.95867
## [233] 11.93528 11.91114 11.88656 11.86184 11.83729 11.81323 11.78996 11.76779
## [241] 11.74703 11.72798 11.71096 11.69626 11.68321 11.67089 11.65933 11.64852
## [249] 11.63848 11.62922 11.62075 11.61307 11.60620 11.60013 11.59490 11.59049
## [257] 11.58692 11.58421 11.58235 11.58136 11.58244 11.58652 11.59318 11.60202
## [265] 11.61265 11.62464 11.63760 11.65112 11.66479 11.67822 11.69099 11.70270
## [273] 11.71294 11.72131 11.72907 11.73771 11.74714 11.75728 11.76806 11.77938
## [281] 11.79118 11.80336 11.81584 11.82855 11.84141 11.85433 11.86722 11.88002
## [289] 11.89264 11.90499 11.91701 11.92859 11.93967 11.95117 11.96398 11.97796
## [297] 11.99297 12.00888 12.02553 12.04279 12.06053 12.07860 12.09686 12.11517
## [305] 12.13341 12.15141 12.16905 12.18619 12.20268 12.21839 12.23318 12.24691
## [313] 12.25943 12.27062 12.28110 12.29160 12.30205 12.31242 12.32264 12.33269
## [321] 12.34250 12.35203 12.36123 12.37006 12.37846 12.38639 12.39379 12.40064
## [329] 12.40686 12.41242 12.41690 12.42003 12.42200 12.42300 12.42320 12.42281
## [337] 12.42201 12.42098 12.41990 12.41898 12.41839 12.41832 12.41896 12.42050
## [345] 12.42220 12.42327 12.42376 12.42376 12.42333 12.42253 12.42144 12.42012
## [353] 12.41864 12.41707 12.41547 12.41392 12.41247 12.41121 12.41020 12.40950
## [361] 12.40919 12.40933 12.40999 12.41037 12.40973 12.40827 12.40617 12.40361
## [369] 12.40079 12.39787 12.39505 12.39252 12.39045 12.38903 12.38845 12.38889
## [377] 12.39053 12.39344 12.39747 12.40246 12.40828 12.41477 12.42179 12.42919
## [385] 12.43684 12.44458 12.45227 12.45976 12.46691 12.47358 12.47961 12.48487
## [393] 12.48920 12.49381 12.49984 12.50702 12.51508 12.52376 12.53280 12.54192
## [401] 12.55087 12.55938 12.56718 12.57401 12.57960 12.58370 12.58603 12.58719
## [409] 12.58796 12.58836 12.58841 12.58812 12.58753 12.58663 12.58547 12.58404
## [417] 12.58238 12.58050 12.57842 12.57615 12.57373 12.57117 12.56848 12.56568
## [425] 12.56280 12.55986 12.55601 12.55056 12.54372 12.53572 12.52678 12.51712
## [433] 12.50695 12.49651 12.48600 12.47566 12.46570 12.45633 12.44779 12.44029
## [441] 12.43406 12.42931 12.42513 12.42053 12.41556 12.41028 12.40477 12.39907
## [449] 12.39326 12.38740 12.38155 12.37578 12.37014 12.36471 12.35954 12.35470
## [457] 12.35025 12.34625 12.34277 12.33987 12.33761 12.33618 12.33563 12.33584
## [465] 12.33671 12.33811 12.33993 12.34205 12.34436 12.34675 12.34909 12.35127
## [473] 12.35318 12.35469 12.35570 12.35654 12.35760 12.35886 12.36030 12.36191
## [481] 12.36367 12.36556 12.36756 12.36966 12.37185 12.37409 12.37637 12.37868
## [489] 12.38100 12.38331 12.38560 12.38793 12.39040 12.39300 12.39572 12.39853
## [497] 12.40145 12.40445 12.40752 12.41066 12.41385 12.41709 12.42036 12.42365
## [505] 12.42696 12.43030 12.43372 12.43720 12.44076 12.44439 12.44809 12.45186
## [513] 12.45570 12.45962 12.46360 12.46766 12.47179 12.47599 12.48026 12.48460
## [521] 12.48901 12.49350 12.49805 12.50268 12.50738 12.51215 12.51699 12.52192
## [529] 12.52692 12.53200 12.53716 12.54239 12.54771 12.55310 12.55856 12.56410
## [537] 12.56972 12.57541 12.58118 12.58702
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")